#!/bin/bash
set -e

node scripts/remove-tests-tsconfig.cjs
# rm -rf dist && npx microbundle --tsconfig tsconfig.json --no-sourcemap && tsc-alias -p tsconfig.json
rimraf dist && tsc --project tsconfig.json &&  rollup -c --bundleConfigAsCjs && tsc-alias -p tsconfig.json 


# Generate index.d.mts for ESM compatibility
cp dist/index.d.ts dist/index.d.mts

# Post-process files to fix import paths and compatibility
# node scripts/postprocess-files.cjs

# Create package.json for dist folder
node scripts/make-dist-package-json.cjs > dist/package.json

# make sure that nothing crashes when we require the output CJS or
# import the output ESM
(cd dist && node -e 'require("jamaibase")')
(cd dist && node -e 'import("jamaibase")' --input-type=module)

# include "__tests__" folder in tsconfig to facilitate unit test.
node scripts/include-tests-tsconfig.cjs


# npm exec tsc-multi
# node scripts/fix-index-exports.cjs
# cp dist/index.d.ts dist/index.d.mts
# cp tsconfig.dist-src.json dist/src/tsconfig.json

# node scripts/postprocess-files.cjs



# if command -v deno &> /dev/null && [ -e ./build-deno ]
# then
#   ./build-deno
# fi

# rm -rf dist
# mkdir -p dist

# cp -r src dist/

# cp *.js dist/
# cp *.json dist/

# tsc --project tsconfig.build.json --traceResolution
